package net.pws.common.persistence.jdbc;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.sql.DataSource;

import net.pws.common.persistence.IPage;
import net.pws.common.persistence.Page;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.util.Assert;


public class JdbcTemplateMysqlExtension extends JdbcTemplateExtension {
    
    public JdbcTemplateMysqlExtension() {
        super();
    }
    
    public JdbcTemplateMysqlExtension(DataSource dataSource, boolean lazyInit) {
        super(dataSource, lazyInit);
    }
    
    public JdbcTemplateMysqlExtension(DataSource dataSource) {
        super(dataSource);
    }

    public IPage queryByPage(final String sql4Query,
                            final Object[] parameters,
                            final int startPageNo,
                            final int maxResultsInPage,
                            final RowMapper rowMapper) {
        return queryByPage(getSql4QueryCount(sql4Query),
                           sql4Query,
                           parameters,
                           startPageNo,
                           maxResultsInPage,
                           rowMapper);
    }

    public IPage queryByPage(final String sql4Query,
                            final List parameters,
                            final int startPageNo,
                            final int maxResultsInPage,
                            final RowMapper rowMapper) {
        return queryByPage(getSql4QueryCount(sql4Query),
                           sql4Query,
                           parameters.toArray(),
                           startPageNo,
                           maxResultsInPage,
                           rowMapper);
    }
    
    public IPage queryByPage(final String sql4Count,
                            final String sql4Query,
                            final Object[] parameters,
                            final int startPageNo,
                            final int maxResultsInPage,
                            final RowMapper rowMapper) {
        Assert.hasText(sql4Count, "用于计数的sql不能为空!");
        Assert.hasText(sql4Query, "用于查询的sql不能为空！");
        Assert.isTrue(startPageNo >= 0, "起始页不能小于0！");
        Assert.notNull(rowMapper, "rowMapper不能为nul！");
        if (logger.isDebugEnabled()) {
            logger.debug("Executing SQL query 4 count" + (sql4Count != null ? " [" + sql4Count
                                                                              + "]"
                                                                           : ""));
        }
        long totalCount = queryForLong(sql4Count, parameters);
        // ����¼����С��1�򷵻ؿյ�Page
        if (totalCount < 1) {
            return new Page(0, 0, maxResultsInPage, Collections.EMPTY_LIST);
        }
        int startIndex = Page.getStartOfPage(startPageNo, maxResultsInPage);
        List parameterArray = new ArrayList();
        for (int i = 0; i < parameters.length; i++) {
            parameterArray.add(parameters[i]);
        }
        // List parameterArray = Arrays.asList(parameters);
        // List parameterArray = new ArrayList(parameters);
        parameterArray.add(new Integer(startIndex));
        parameterArray.add(new Integer(maxResultsInPage));
        if (logger.isDebugEnabled()) {
            logger.debug("Executing SQL query 4 page" + (getSql4QueryPage(sql4Query) != null ? " [" + getSql4QueryPage(sql4Query)
                                                                                               + "]"
                                                                                            : ""));
        }
        List list = query(getSql4QueryPage(sql4Query),
                          parameterArray.toArray(),
                          rowMapper);
        return new Page(startPageNo, totalCount, maxResultsInPage, list);
    }
    
    private String getSql4QueryCount(final String sql4Query) {
        StringBuffer result = new StringBuffer(" select count(*) from ( ");
        result.append(sql4Query);
        result.append(" ) count_ ");
        return result.toString();
    }
    
    private String getSql4QueryPage(final String sql4Query) {
        StringBuffer result = new StringBuffer(" select * from ( ");
        result.append(sql4Query);
        result.append(" ) row_ limit ?, ? ");
        return result.toString();
    }
    
}
